/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.entitybeans.user;

import com.bionic.ce.entities.User;
import com.bionic.ce.entitybeans.AbstractFacade;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Jarik
 */
@Stateless
public class UserFacade extends AbstractFacade<User> implements UserFacadeLocal {

    @PersistenceContext(unitName = "Currency_ExchangeEE-PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }

    public User findById(Integer id) {
        TypedQuery<User> query = em.createNamedQuery("User.findById", User.class);
        query.setParameter("id", id);
        User user = null;
        try {
            user = query.getSingleResult();
        } catch (NoResultException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public List<User> findByDynamicSelect(User user) {
        String queryString = "SELECT u FROM User u";
        int i = 0;
        if (user.getId() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "id=" + user.getId();
            i++;
        }
        if (user.getName() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "Name=\'" + user.getName() + "\'";
            i++;
        }
        if (user.getSurName() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "SurName=\'" + user.getSurName() + "\'";
            i++;
        }
        if (user.getEmail() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "email=\'" + user.getEmail() + "\'";
            i++;
        }
        if (user.getPassword() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "Password=\'" + user.getPassword() + "\'";
            i++;
        }
        if (user.getAdress() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "Adress=\'" + user.getAdress() + "\'";
            i++;
        }
        if (user.getTelephone() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "Telephone=\'" + user.getTelephone() + "\'";
            i++;
        }
        if (user.getStatus() != null) {
            if (i == 0) queryString += " WHERE ";
            else if (i > 0) queryString += " AND ";
            queryString += "Status=\'" + user.getStatus() + "\'";
            i++;
        }

        //queryString += ";";
        TypedQuery<User> tpquery = em.createQuery(queryString, User.class);

        List<User> userlist = null;
        try {
            userlist = tpquery.getResultList();
        } catch (NoResultException e) {
            e.printStackTrace();
        }
        return userlist;
    }
}
